package org.jackysoft.workflow;

import java.util.Calendar;

import org.apache.commons.collections.Transformer;
import org.jackysoft.entity.HibernateStep;
import org.jackysoft.opensymphony.workflow.entity.ActionDescriptor;
import org.jackysoft.opensymphony.workflow.entity.WorkflowDescriptor;
import org.jackysoft.service.VariableService;
import org.jackysoft.service.WorkflowService;
import org.jackysoft.util.DateUtils;
import org.jackysoft.util.WorkflowConstant;
import org.jackysoft.util.WorkflowUtil;
import org.joda.time.DateTime;

public class Step2WorkItemTransformer implements Transformer {
	private VariableService workflowVariableService;
	private WorkflowService service;
	public Step2WorkItemTransformer(VariableService workflowVariableService) {
		this.workflowVariableService = workflowVariableService;
	}
	public Step2WorkItemTransformer(VariableService workflowVariableService,
			WorkflowService service) {
		this.service = service;
		this.workflowVariableService = workflowVariableService;
	}

	@Override
	public Object transform(Object arg0) {
		HibernateStep step = (HibernateStep) arg0;
	    long entryid = step.getEntryId();
	    String wname = step.getEntry().getWorkflowName();
		WorkflowDescriptor wdesc = service.getWorkflowDescriptor(wname);
		if(wdesc==null)return null;
		String sender = WorkflowUtil.getVarStringValue(workflowVariableService.get(entryid
				,WorkflowConstant.LAST_EXECUTOR.toString()));
		String senderId =  WorkflowUtil.getVarStringValue(workflowVariableService.get(entryid
				,WorkflowConstant.LAST_EXECUTOR_USERID.toString()));
		String starter =  WorkflowUtil.getVarStringValue(workflowVariableService.get(step.getEntryId()
				, WorkflowConstant.STARTER.toString()));
		String starterId =  WorkflowUtil.getVarStringValue(workflowVariableService.get(step.getEntryId()
				, WorkflowConstant.STARTER_USERID.toString()));
		String fname =  WorkflowUtil.getVarStringValue(workflowVariableService.get(step.getEntryId()
				, WorkflowConstant.FORM_TITLE.toString()));
		
		DateTime sendDate =  WorkflowUtil.getVarDateValue(workflowVariableService.get(step.getEntryId()
				, WorkflowConstant.SEND_DATE.toString()));
		sendDate = sendDate==null?sendDate=DateTime.now():sendDate;
		ActionDescriptor adesc = wdesc.getAction(step.getActionId()); 
		String actionName = adesc==null?"":adesc.getName();
		WorkItem item = new WorkItem();
        item.setTitle(fname);
		item.setActionId(step.getActionId());
		item.setActionName(actionName);
		item.setFinishDate(DateUtils.formatDate(step.getFinishDate()));
		item.setGlobalStepId(step.getId());
		item.setOwner(step.getOwner());
		item.setPrevIds(step.getPreviousStepIds());
		item.setStarter(starter);
		item.setStarterName(starterId);
		item.setStartDate(DateUtils.formatDate(step.getStartDate()));
		item.setSender(sender);
		item.setSendDate(sendDate.toString());
		item.setStatus(step.getStatus());
		item.setState(step.getEntry().getState());
		item.setStepId(step.getStepId());
		item.setWorkflowId(entryid);
		item.setWorkflowName(wname);
		item.setSenderId(senderId);
		return item;
	}

	
}
